<template>
  <div class="config-manager">
    <div class="config-manager-title">
      <span>工作空间</span>
      <b>随意切换：个人项目 | 公司项目 / pc | 移动端</b>
      <div><span @click="setHosts">保存</span></div>
    </div>
    <div class="config-manager-list">
      <div>
        <div class="switch-work-space">
          <span
            v-for="(hosts, index) in hostsList"
            :index="index"
            :class="{active: index === currentHostsIndex}"
            @click="setCurrentHosts(index, hosts)"
          >{{hosts.name}}</span>
          <span @click="addNewHostsList">添加</span>
        </div>
        <div class="switch-repertory">
          <ul>
            <li v-if="hosts && hosts.ip !== undefined" v-for="(hosts, index) in currentHosts && currentHosts.content"  :key="index" :class="{active: hosts.isEditing}">
              <div :class="{active: !hosts.disabled}" @click="hosts.disabled = !hosts.disabled"></div>
              <div><input type="text" v-model="hosts.name" placeholder="域名" :disabled="!hosts.isEditing"></div>
              <div><input type="text" v-model="hosts.ip" placeholder="地址" :disabled="!hosts.isEditing"></div>
              <div>
                <span @click="hosts.isEditing = !hosts.isEditing">{{hosts.isEditing ? "完成" : "编辑"}}</span>
                <span @click="deleteHosts(hosts, currentHosts.content)">删除</span>
              </div>
            </li>
          </ul>
          <div class="add-new">
            <span @click="changeName">改名</span>
            <span @click="deleteHosts(currentHosts, hostsList)">删除</span>
            <span @click="addNewHosts"></span>
          </div>
        </div>
      </div>
    </div>
    <div class="logs">
      <ul>
        <li v-for="item in serveMsg">{{item}}</li>
      </ul>
    </div>
    <PlatPopup :conf="popConf">
      <ChangeNameForm :conf="popConf"/>
    </PlatPopup>
  </div>
</template>
<script>
import devAjax from "../../devAjax";
import {parseObjToTree, parseTreeToObj, parseObjectToTreeTools} from "cdy-utils";
import PlatformTree from "../../units/PlatformTree";
import PlatPopup from "../../units/PlatPopup";
import ChangeNameForm from "../../units/ChangeNameForm";
import GitDiffForm from "../../units/GitDiffForm";

export default {
  name: 'home',
  components: {PlatformTree, PlatPopup, ChangeNameForm, GitDiffForm},
  data() {
    return {
      popConf: {
        show: false,
        full: true,
        space: null,
        brances: null,
        onConfirm: (name, hide) => {
          this.currentHosts.name = name;
          this.setHosts();
          hide();
        },
      },
      serveMsg: [],
      isGitCommittingAll: false,
      isGettingLatestCodes: false,
      isApplyingSetting: false,
      isInstallingDependence: false,
      hostsList: [],
      currentHostsIndex: 0,
    }
  },
  computed: {
    currentHosts(){
      return this.hostsList[this.currentHostsIndex]
    },
  },
  mounted() {
    this.getHosts();
  },
  methods: {
    log() {
      console.log.apply(console, arguments);
    },
    changeName(){
      this.popConf.show = true;
    },
    deleteHosts(hosts, curHosts){
      curHosts.splice(curHosts.indexOf(hosts), 1);
      this.setHosts();
    },
    setCurrentHosts(index) {
      this.currentHostsIndex = index;
    },
    setHosts(){
      const cur = this.hostsList[this.currentHostsIndex];
      devAjax.devToolSetHosts({hosts: this.hostsList.filter(item => item.isEnv), cur}).then(res => {
        this.getHosts();
      });
    },
    addNewHosts(){
      this.currentHosts.content.push({disabled: true, ip: "", isEditing: false, name: "",});
    },
    addNewHostsList(){
      this.hostsList.push({
        content: [],
        isEnv: true,
        name: "new",
      })
    },
    getHosts() {
      devAjax.devToolGetHosts().then(res => {
        const hostsList = res.data.data.custom;
        hostsList.push({
          isEnv: false,
          name: "local",
          content: res.data.data.local
        });
        hostsList.forEach(
          item => {
            item && item.content && item.content.forEach(item =>
              item && item.ip && (item.isEditing = false)
            )
          }
        );
        this.hostsList = res.data.data.custom;
      });
    },
  },
  watch: {},
}
</script>

<style scoped>
  .switch-repertory {
    padding-top: 10px;
  }

  .switch-repertory > span {
    display: block;
    line-height: 30px;
    margin: 0 14px;
    padding: 0 10px;
    background: #65aed8;
    color: #fff;
    /*border-top: 1px solid #eee;*/
    /*border-bottom: 1px solid #eee;*/
  }

  .switch-repertory > ul {
    padding: 5px 10px;
  }

  .switch-repertory > ul > li {
    display: flex;
    padding: 4px 0;
    overflow: hidden;
  }

  .switch-repertory > ul > li > div {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1 1 0;
    padding: 4px 10px;
    border: 1px solid #f0f0f0;
    margin: 0 4px;
    transition: all .3s;
  }

  .switch-repertory > ul > li.active > div {
    border: 1px solid rgba(40, 112, 186, .6);
  }

  .switch-repertory > ul > li > div:nth-child(1) {
    flex: 0 0 auto;
    padding: 7px;
  }
  .switch-repertory > ul > li > div:nth-child(1):before{
    background-color: #fff;
    border: 1px solid #2870ba;
    content: "";
    display: block;
    height: 100%;
    width: 14px;
    border-radius: 2px;
    transition: all .3s;
  }
  .switch-repertory > ul > li > div:hover:nth-child(1):before{
    background-color: rgba(40, 112, 186, .6);
  }
  .switch-repertory > ul > li > div.active:nth-child(1):before{
    background-color: #2870ba;
  }
  .switch-repertory > ul > li > div:nth-child(2) {
    flex: 2 2 0;
  }

  .switch-repertory > ul > li > div:nth-child(3) {
    flex: 3 3 0;
  }

  .switch-repertory > ul > li > div:nth-child(4) {
    flex: 1.5 1.5 0;
  }

  .switch-repertory > ul > li > div > span {
    background: #2870bc;
    color: #fff;
    padding: 0 6px;
    border-radius: 2px;
    opacity: .7;
    transition: all .3s;
  }

  .switch-repertory > ul > li > div > span:hover {
    opacity: 1;
  }

  .switch-repertory > ul > li > div > span:active {
    color: #2870bc;
    background-color: #fff;
  }

  .config-manager-list {
    flex: 1 1 0;
    overflow: hidden;
  }

  .config-manager-list > div {
    width: 100%;
    height: 100%;
    overflow: scroll;
  }

  .config-manager-list > div::-webkit-scrollbar {
    background: transparent;
  }

  .config-manager {
    background-color: #fff;
    padding: 4px;
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  .config-manager-title {
    display: flex;
    border-left: 3px solid #0099cc;
    line-height: 30px;
    margin-bottom: 20px;
  }

  .config-manager-title > span {
    padding: 0 10px;
    font-size: 14px;
  }

  .config-manager-title > b {
    color: #ccc;
    font-size: 12px;
    font-weight: normal;
  }

  .config-manager-title > div {
    flex: 1 1 0;
    text-align: right;
  }

  .config-manager-title > div > span {
    display: inline-block;
    line-height: 24px;
    margin: 4px 0;
    background-color: #2870ba;
    color: #fff;
    padding: 0 20px;
    border-radius: 4px;
  }

  .switch-work-space {
    display: flex;
    justify-content: center;
    background-color: #2870ba;
    padding: 10px;
    height: 50px;
    border-radius: 4px;
  }

  .switch-work-space > span {
    display: block;
    padding: 0 20px;
    line-height: 30px;
    height: 30px;
    min-width: 90px;
    text-align: center;
    background-color: rgba(255, 255, 255, 1);
    cursor: pointer;
    transition: all .3s;
    transform: scale(.9, .9);
    border-radius: 4px;
  }

  .switch-work-space > span:hover {
    background-color: rgba(255, 255, 255, .8);
  }

  .switch-work-space > span:active {
    transform: scale(1, 1);
  }

  .switch-work-space > span.active {
    background-color: rgba(255, 255, 255, .3);
    color: #fff;
  }

  .switch-work-space > span:first-child {
    /*flex: 1 1 0;*/
  }

  .switch-work-space > span:last-child {
    /*flex: 2 2 0;*/
  }

  .logs {
    width: 100%;
    height: 100px;
    overflow: hidden;
    background-color: #fff;
    position: relative;
  }

  .logs > ul {
    position: absolute;
    bottom: 0;
    left: 0;
  }

  .logs > ul > li {
    padding: 4px 10px;
  }
  .add-new{
    margin-top: 20px;
    height: 30px;
    text-align: right;
  }
  .add-new span{
    display: inline-block;
    line-height: 27px;
    vertical-align: middle;
  }
  .add-new span:not(:last-child){
    margin-right: 8px;
    background-color: #2870ba;
    color: #fff;
    padding: 0 20px;
    border-radius: 4px;
    transform: scale(.9, .9);
    transition: all .3s;
  }
  .add-new span:not(:last-child):hover{
    transform: scale(1, 1);
  }
  .add-new span:not(:last-child):active{
    background-color: #fff;
    color: #2870ba;
  }
  .add-new span:last-child{
    position: relative;
    display: inline-block;
    height: 30px;
    width: 30px;
    background-color: #2870ba;
    border-radius: 4px;
    transition: all .3s;
    transform: scale(.9, .9);
  }
  .add-new span:last-child:hover{
    transform: scale(1, 1);
  }
  .add-new span:last-child:active{
    background-color: #fff;
  }
  .add-new span:last-child:before,
  .add-new span:last-child:after{
    position: absolute;
    content: "";
    display: block;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    transition: all .3s;
  }
  .add-new span:last-child:active:before,
  .add-new span:last-child:active:after{
    background-color: #2870ba;
  }
  .add-new span:last-child:before{
    width: 80%;
    height: 4px;
  }
  .add-new span:last-child:after{
    height: 80%;
    width: 4px;
  }
</style>
